package com.yin.project.admin.rbac.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yin.project.admin.rbac.model.EasyTreeNode;
import com.yin.project.admin.rbac.model.MenuEntity;
import com.yin.project.admin.rbac.service.MenuService;

/**
 * Created by Eason on 2016/12/1.
 */
@Controller
@RequestMapping(value = "/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    /**
     * 查询所有菜单
     * @return
     */
    @RequestMapping(value = "/_", method = RequestMethod.GET)
    @ResponseBody
    public List<EasyTreeNode> getAll() {
        List<MenuEntity> menus  = menuService.queryMenusByParams();
        List<EasyTreeNode> trees = new ArrayList<EasyTreeNode>();
        Map<String, Object> treeMap = new HashMap<String, Object>();
        EasyTreeNode easyTreeNode = null;
        List<EasyTreeNode> chilreNodes = new ArrayList<EasyTreeNode>();
        if(menus!=null&&menus.size()>0){
        	for (MenuEntity menuEntity : menus) {
				if(menuEntity.getParent()==null){
					easyTreeNode =new EasyTreeNode(menuEntity.getId(),menuEntity.getMenuName(),false,"",null,"close","");
					trees.add(easyTreeNode);
					treeMap.put(menuEntity.getId(), easyTreeNode);
				}else{
					easyTreeNode = new EasyTreeNode(menuEntity.getId(), menuEntity.getMenuName(), false, menuEntity.getParent(),null, "close",menuEntity.getUrl());
					if (treeMap!=null&&treeMap.size()>0) {
						convertMenuToTreeNode(treeMap, easyTreeNode, chilreNodes);
					}else{
						chilreNodes.add(easyTreeNode);
						treeMap.put(menuEntity.getId(), easyTreeNode);
					}
				}
			}
        	if(chilreNodes!=null&&chilreNodes.size()>0){
        		for (EasyTreeNode easyTree : chilreNodes) {
        			convertMenuToTreeNode(treeMap, easyTree, chilreNodes);
				}
        	}
        }
        return trees;
    }
    public List<EasyTreeNode> convertMenuToTreeNode(Map<String, Object> treeMap,EasyTreeNode easyTreeNode,List<EasyTreeNode> chilreNodes){
    	if (treeMap!=null&&treeMap.size()>0) {
			EasyTreeNode treeNode =(EasyTreeNode) treeMap.get(easyTreeNode.getParentId());
			if(treeNode!=null){
				if(treeNode.getChildren()!=null&&treeNode.getChildren().size()>0){
					treeNode.getChildren().add(easyTreeNode);
				}else {
					List<EasyTreeNode> childrenList = new ArrayList<EasyTreeNode>();
					childrenList.add(easyTreeNode);
					treeNode.setChildren(childrenList);
				}
			}else {
				chilreNodes.add(easyTreeNode);
				treeMap.put(easyTreeNode.getId(), easyTreeNode);
			}
		}
		return chilreNodes;
    }
    /**
     * 菜单列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<MenuEntity> menuList(){
    	List<MenuEntity> menus  = menuService.queryMenusByParams();
    	return menus;
    }
    /**
     * 保存菜单
     * @return
     */
    @RequestMapping(value = "/_", method = RequestMethod.POST)
    @ResponseBody
    public String addMenu(@ModelAttribute("menu") MenuEntity menu){
    	if(StringUtils.isNotEmpty(menu.getMenuCode()) && StringUtils.isNotEmpty(menu.getMenuName())){
			menuService.addMenu(menu);
			return "ok";
    	}else{
    		return "error";
    	}
    }
    /**
     * 根据ID获取菜单信息
     * @return
     */
    @RequestMapping(value = "/_/{id}", method = RequestMethod.GET)
    @ResponseBody
    public MenuEntity updateMenuView(@PathVariable("id") String id) {
    	MenuEntity menuEntity  = menuService.queryMenusById(id);
        return menuEntity;
    }
    /**
     * 修改菜单
     * @return
     */
    @RequestMapping(value = "/_/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public String updateMenu(@PathVariable("id") String id,@RequestBody String body){
    	MenuEntity menuEntity =JSON.parseObject(body, MenuEntity.class);
    	menuEntity.setId(id);
    	menuService.updateMenu(menuEntity);
        return "ok";
    }
    /**
     * 删除菜单
     * @return
     */
    @RequestMapping(value = "/_/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteMenu(@PathVariable("id") String id){
    	menuService.deleteMenu(id);
    	return "ok";
    }
}
